package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.RegisterDAO;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Register;
import com.wanmait.hotelManage.vo.Room;

@WebServlet("/manage/RoomHistoryServlet")
public class RoomHistoryServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if("list".equals(action)) {
			this.list(request, response);
		}else if("getRegisters".equals(action)) {
			this.getRegisters(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("getSearchRegisters".equals(action)) {
			this.getSearchRegisters(request, response);
		}
	}
	//搜索后分页
	public void getSearchRegisters(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("pageNo"));
		String roomNumber = request.getParameter("roomNumber");
		String idCard = request.getParameter("idCard");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		Date st=null;
		Date et=null;
		try {
			st = df.parse(startTime);
		} catch (ParseException e) {	
		}
		try {
			et = df.parse(endTime);
		} catch (ParseException e) {	
		}
		Timestamp stt=null;
		Timestamp ett=null;
		try {
			stt = new Timestamp(st.getTime());
		} catch (Exception e) {
		}
		try {
			ett = new Timestamp(et.getTime());
		} catch (Exception e) {
		}
		Register register=new Register();
		register.setEndTime(ett);
		register.setIdCard(idCard);
		register.setStartTime(stt);
		Room room=new Room();
		room.setNumber(roomNumber);
		register.setRoom(room);
		
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(pageNo);
		int rows=RegisterDAO.getRegisterdao().getHistoryRows(register);
		pager.setPageCount(rows);
		
		List<Register> registers = RegisterDAO.getRegisterdao().searchRegisters(register,pager);
		request.setAttribute("pager", pager);
		request.setAttribute("registers", registers);
//		request.setAttribute(name, o);
//		request.getRequestDispatcher("/manage/roomHistory/showRegisterList.jsp").forward(request, response);

		//response.sendRedirect(request.getContextPath()+"/manage/roomHistory/searchRegisterList.jsp");
		request.getRequestDispatcher("/manage/roomHistory/showSearchRegisterList.jsp").forward(request, response);

	}
	//搜索
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String roomNumber = request.getParameter("roomNumber");
		String idCard = request.getParameter("idCard");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		Date st=null;
		Date et=null;
		try {
			st = df.parse(startTime);
		} catch (ParseException e) {	
		}
		try {
			et = df.parse(endTime);
		} catch (ParseException e) {	
		}
		Timestamp stt=null;
		Timestamp ett=null;
		try {
			stt = new Timestamp(st.getTime());
		} catch (Exception e) {
		}
		try {
			ett = new Timestamp(et.getTime());
		} catch (Exception e) {
		}
		Register register=new Register();
		register.setEndTime(ett);
		register.setIdCard(idCard);
		register.setStartTime(stt);
		Room room=new Room();
		room.setNumber(roomNumber);
		register.setRoom(room);
		
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(1);
		int rows=RegisterDAO.getRegisterdao().getHistoryRows(register);
		pager.setPageCount(rows);
		
		List<Register> registers = RegisterDAO.getRegisterdao().searchRegisters(register,pager);
		
		request.setAttribute("registers", registers);
		request.setAttribute("pager", pager);
		request.setAttribute("register", register);
		//response.sendRedirect(request.getContextPath()+"/manage/roomHistory/searchRegisterList.jsp");
		request.getRequestDispatcher("/manage/roomHistory/searchRegisterList.jsp").forward(request, response);
	}
	//分页
	public void getRegisters(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("pageNo"));
		Pager pager =new Pager();
		pager.setPageSize(8);
		pager.setPageNo(pageNo);
		int rows=RegisterDAO.getRegisterdao().getCheckOutRows();
		pager.setPageCount(rows);
		
		List<Register> registers=RegisterDAO.getRegisterdao().findRoomHistoryRegister(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("registers", registers);
		request.getRequestDispatcher("/manage/roomHistory/showRegisterList.jsp").forward(request, response);
	}
	//列出全部的客史
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(1);
		int rows=RegisterDAO.getRegisterdao().getCheckOutRows();
		pager.setPageCount(rows);
		
		List<Register> registers = RegisterDAO.getRegisterdao().findRoomHistoryRegister(pager);
//		System.out.println(registers.get(0).getName());
		request.setAttribute("pager", pager);
		request.setAttribute("registers", registers);
		request.getRequestDispatcher("/manage/roomHistory/list.jsp").forward(request, response);
	}

}
